#include "my_register.h"
#include "ui_my_register.h"
#include "login.h"
#include "databasemanager.h"

#include <qmessagebox.h>
#include <QDateTime>

my_register* my_register::instance=nullptr;

my_register::my_register(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::my_register)
{
    ui->setupUi(this);

    //隐去上边框
    setWindowFlag(Qt::FramelessWindowHint);

    this->installEventFilter(new DragWidgetFilter_1(this));

    QRegularExpression passwordRegex("^(?=.*[a-z])(?=.*[A-Z])(?=.*[_*#]).{10,14}$");
    QString password=ui->lineEdit_2->text();

    QRegularExpressionValidator *validator_password=new QRegularExpressionValidator(passwordRegex,ui->lineEdit_2);
    ui->lineEdit_2->setValidator(validator_password);

    connect(ui->lineEdit_2,&QLineEdit::textChanged,[=](QString text){
        int pos=0;
        if(validator_password->validate(text,pos)==QValidator::Acceptable)
            {
            ui->lineEdit_2->setStyleSheet("");
        }else {
            ui->lineEdit_2->setStyleSheet("border: 1px solid red;");
        }
    });


}

my_register::~my_register()
{
    delete ui;
}

void my_register::paintEvent(QPaintEvent *event)
{
    QPainter mainPainter(this);
    mainPainter.drawPixmap(rect(),QPixmap(":/login/qq_back.jpeg"),QRect());
}

void my_register::register_successed(int res)
{

    QMessageBox::information(NULL,"提示",QString("你的id是%1").arg(res),QMessageBox::Apply);
    this->on_return_login_pushButton_clicked();
}

my_register *my_register::getInstance()
{
    if(instance==nullptr)
    {
        instance=new my_register();
    }
    return instance;
}

void my_register::destoryInstance()
{
    if(instance)
    {
        qDebug()<<"register delete";
        delete instance;
        instance=nullptr;
    }
}



void my_register::on_radioButton_clicked()
{
    if(ui->radioButton->isChecked()){
        ui->register_pushButton->setStyleSheet("background-color: rgb(0,141,235);");
        ui->register_pushButton->setEnabled(true);
    }
    else {
        ui->register_pushButton->setStyleSheet("background-color: rgb(158,218,255);");
        ui->register_pushButton->setEnabled(false);
    }
}


void my_register::on_close_toolButton_clicked()
{
    this->close();
}


void my_register::on_register_pushButton_clicked()
{
    QString nickname=ui->lineEdit->text();
    QString password=ui->lineEdit_2->text();
    QString phone_num=ui->lineEdit_3->text();
    QString email_num=ui->lineEdit_4->text();
    const QValidator *validator=ui->lineEdit_2->validator();
    int pos=0;
    QValidator::State state= validator->validate(password,pos);
    if(state!=QValidator::Acceptable)
    {
        QMessageBox::warning(NULL,"警告","密码不符合规范",QMessageBox::Apply);

    }
    else if(!nickname.isEmpty()&&!password.isEmpty())
    {
        int res=DatabaseManager::getInstance().insert_register(nickname,password,phone_num,email_num);
        if(res>0)
        {
            register_successed(res);
        }
    }else {
        QMessageBox::warning(NULL,"警告","有空值",QMessageBox::Apply);
    }
}





void my_register::on_return_login_pushButton_clicked()
{
    // emit regSuccess();
    QPoint currentPos=this->pos();

    this->destoryInstance();
    login* lg=login::getInstance();
    lg->move(currentPos);
    lg->show();
}

